package com.sakura.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.dto.DishDto;
import com.sakura.mapper.DishMapper;
import com.sakura.pojo.Dish;
import com.sakura.pojo.DishFlavor;
import com.sakura.serivce.DishFlavorService;
import com.sakura.serivce.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveDishAndDishFlavor(DishDto dishDto) {
        // 保存dish
        this.save(dishDto);

        // 保存dishFlavor
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 保存dishFlavor中的菜品ID
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithDishAndFlavor(Long id) {
        Dish dish = this.getById(id);
        // 根据Dish中的口味ID获取所有关于菜品口味表
        QueryWrapper<DishFlavor> dishFlavorQueryWrapper = new QueryWrapper<>();
        dishFlavorQueryWrapper.eq("dish_id", id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorQueryWrapper);

        // 把dish和dishFlavor都放入dishDto中
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    @Override
    public void updateByIdWithFlavor(DishDto dishDto) {
        // 保存dish数据，Flavor单独处理 select * from dish where id = ?
        this.updateById(dishDto);

        // 删除Flavor中和该菜品中口味的数据，并重新保存 delete from dishFlavor where id = ?
        QueryWrapper<DishFlavor> dishFlavorQueryWrapper = new QueryWrapper<>();
        // 回显ID保存到dishFlavor中Dish_id中
        dishFlavorQueryWrapper.eq("dish_id", dishDto.getId());
        dishFlavorService.remove(dishFlavorQueryWrapper);

        // 处理Flavor，查询所有关于菜品的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void disableById(Integer status, Long[] ids) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            this.updateById(dish);
        }
    }

    @Override
    public List<DishDto> listByCategoryId(Long categoryId) {
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq(categoryId != null, "category_id", categoryId);
        dishQueryWrapper.orderByAsc("sort").orderByDesc("update_time");
        List<Dish> dishList = this.list(dishQueryWrapper);

        ArrayList<DishDto> dishDtoArrayList = new ArrayList<>();
        // 转换成DTO，添加口味List DTO中数据: Dish + flavors
        for (Dish dish : dishList) {
            // 创建临时缓存数据
            DishDto dishDto = new DishDto();
            // 拷贝到DTO中
            BeanUtils.copyProperties(dish,dishDto);
            // 添加条件
            QueryWrapper<DishFlavor> dishFlavorQueryWrapper = new QueryWrapper<>();
            dishFlavorQueryWrapper.eq("dish_id",dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorQueryWrapper);
            // 添加到DTO中的口味变量
            dishDto.setFlavors(dishFlavorList);
            dishDtoArrayList.add(dishDto);
        }

        return dishDtoArrayList;
    }
}
